package com.hui.security.sys.controller;

import com.hui.security.common.response.ResultMapper;
import com.hui.security.common.response.ResultVO;
import com.hui.security.sys.entity.Group;
import com.hui.security.sys.entity.User;
import com.hui.security.sys.service.GroupService;
import com.hui.security.sys.service.UserService;
import com.hui.security.sys.vo.GroupVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <b><code>GroupController</code></b>
 * <p/>
 * Description:
 * <p/>
 * <b>Creation Time:</b> 2019/1/5 0:19.
 *
 * @author HuWeihui
 */
@RestController
@RequestMapping("/v1")
@Api(value = "用户组API",tags = "user-groups")
@Slf4j
public class GroupController {
    /**
     * The Group service.
     */
    @Autowired
    private GroupService groupService;

    /**
     * Create group result vo.
     *
     * @param groupVO       the group vo
     * @param bindingResult the binding result
     * @return the result vo
     * @author : Hu weihui
     * @since hui_project v1
     */
    @ApiOperation(value = "创建用户组")
    @PostMapping("/group")
    public ResultVO createGroup(@RequestBody @Validated GroupVO groupVO, BindingResult bindingResult){
        Group group = new Group();
        BeanUtils.copyProperties(groupVO,group);
        return ResultMapper.ok(groupService.add(group));
    }

    /**
     * Delete group result vo.
     *
     * @param groupId the group id
     * @return the result vo
     * @author : Hu weihui
     * @since hui_project v1
     */
    @ApiOperation(value = "删除用户组")
    @DeleteMapping("/group/{id}")
    public ResultVO deleteGroup(@PathVariable("id") String groupId){
        groupService.delete(groupId);
        return ResultMapper.ok();
    }

    /**
     * Update group result vo.
     *
     * @param groupVO       the group vo
     * @param bindingResult the binding result
     * @return the result vo
     * @author : Hu weihui
     * @since hui_project v1
     */
    @ApiOperation(value = "更新用户组")
    @PutMapping("/group")
    public ResultVO updateGroup(@RequestBody @Validated GroupVO groupVO, BindingResult bindingResult){
        Group group = new Group();
        BeanUtils.copyProperties(groupVO,group);
        groupService.update(group);
        return ResultMapper.ok();
    }

    /**
     * Gets group.
     *
     * @param groupId the group id
     * @return the group
     * @author : Hu weihui
     * @since nile -szcst 0.1.0
     */
    @ApiOperation(value = "查询用户组详情")
    @GetMapping("/group/{id}")
    public ResultVO getGroup(@PathVariable("id") String groupId){
        Group group = groupService.get(groupId);
        return ResultMapper.ok(group);
    }


}
